package com.bigevent.mapper.user;

import com.bigevent.pojo.Article;
import com.bigevent.pojo.Category;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface HostListMapper {

    @Select("SELECT id, title, content, cover_img, state, category_id, create_user, create_time, update_time, like_count, view_count, (like_count + view_count) AS total_interactions FROM article WHERE state = '已发布' ORDER BY total_interactions DESC LIMIT 10;")
    List<Article> selectHostList();

    @Select("SELECT a.id, a.title, a.content, a.create_user,a.cover_img, u.nickname,a.create_time, a.update_time,a.like_count,a.view_count FROM article a JOIN user u ON a.create_user = u.id WHERE a.id = #{id};")
    Article selectHostDetail(Integer id);

    @Update("UPDATE article SET view_count = view_count + 1 WHERE id = #{id};")
    void updateViewCount(Integer id);

    @Update("UPDATE article SET like_count = like_count + 1 WHERE id = #{id};")
    void like(Integer id);

    @Insert("INSERT INTO user_favorites (user_id, article_id,created_at) VALUES (#{userId}, #{id},#{data});")
    void addLike(Integer userId, Integer id, LocalDateTime data);

    @Select("SELECT COUNT(*) FROM user_favorites WHERE user_id = #{userId} AND article_id = #{id}")
    Integer selectLike(Integer userId, Integer id);

    @Delete("DELETE FROM user_favorites WHERE user_id = #{userId} AND article_id = #{id}")
    void removeLike(Integer userId, Integer id);

    @Update("UPDATE article SET like_count = like_count - 1 WHERE id = #{id};")
    void disLike(Integer id);

    @Select("SELECT * FROM article")
    List<Article> selectAll();

    @Select("SELECT a.id, a.title, a.content, a.cover_img, a.state, a.like_count, a.view_count, a.category_id, a.create_user, a.create_time, a.update_time FROM article a JOIN user_favorites uf ON a.id = uf.article_id WHERE uf.user_id = #{userId};")
    List<Article> selectLikeList(Integer userId);
}
